package order;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import control.ServerIp;

public class OrderInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public OrderInsertServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//request.setCharacterEncoding("euc-kr");
		request.setCharacterEncoding("utf-8");
		
		ServerIp ip = new ServerIp();
		String orderAddSql = "";
		HttpSession session = request.getSession();

		Connection conn = null;		
		PreparedStatement pstmtOrder = null;

		int oi_no;
		int mb_no;
		String oi_date;
		int oi_price;
		String oi_addr;
		int oi_step = 1;
		String  oi_step1_date;
		int oi_total_price;
		
		String list_gs_code;
		String list_gs_count;
		String list_gs_price;
		String list_gs_color;
		String list_gs_size;

		try{
			//Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName(ip.getDriver());
			conn = DriverManager.getConnection(ip.getUrl(),ip.getId(),ip.getPw());
			
			oi_no = SequenceManager.nextId(conn, "ORDER_INFO");
			mb_no = Integer.parseInt((String)session.getAttribute("SESSION_MB_NO"));
			oi_date =  new Timestamp(System.currentTimeMillis()).toString();
			oi_price = Integer.parseInt(request.getParameter("OI_PRICE"));
			oi_addr = request.getParameter("OI_ADDR");
			oi_step = 1;
			oi_step1_date = oi_date;
			oi_total_price = Integer.parseInt(request.getParameter("OI_TOTAL_PRICE"));
			
			orderAddSql = orderAddSql + "insert into order_info (oi_no, mb_no, oi_date, oi_price, oi_addr, oi_step, oi_step1_date, oi_total_price) values ( '";
			orderAddSql = orderAddSql + oi_no + "' , '";
			orderAddSql = orderAddSql + mb_no + "' , '";
			orderAddSql = orderAddSql + oi_date + "' , '";
			orderAddSql = orderAddSql + oi_price + "' , '";
			orderAddSql = orderAddSql + oi_addr + "' , '";
			orderAddSql = orderAddSql + oi_step + "' , '";
			orderAddSql = orderAddSql + oi_step1_date + "' , '";
			orderAddSql = orderAddSql + oi_total_price;
			orderAddSql = orderAddSql + "' ) ";
			
			pstmtOrder = conn.prepareStatement(orderAddSql);
			pstmtOrder.executeUpdate();
			//System.out.println(orderAddSql);
			
			list_gs_code = request.getParameter("LIST_GS_CODE");
			list_gs_count = request.getParameter("LIST_GS_COUNT");
			list_gs_price = request.getParameter("LIST_GS_PRICE");
			list_gs_color = request.getParameter("LIST_GS_COLOR");
			list_gs_size = request.getParameter("LIST_GS_SIZE");
			
			StringTokenizer tokGsCode = new StringTokenizer(list_gs_code, "|");
			StringTokenizer tokGsCount = new StringTokenizer(list_gs_count, "|");
			StringTokenizer tokGsPrice = new StringTokenizer(list_gs_price, "|");
			StringTokenizer tokGsColor = new StringTokenizer(list_gs_color, "|");
			StringTokenizer tokGsSize = new StringTokenizer(list_gs_size, "|");
			
			String tokTmpCode = "";
			String tokTmpCount = "";
			String tokTmpPrice = "";
			String tokTmpColor = "";
			String tokTmpSize = "";
			int tmpTotPrice = 0;
			
			while (tokGsCode.hasMoreTokens()){
				tokTmpCode = tokGsCode.nextToken();
				tokTmpCount = tokGsCount.nextToken();
				tokTmpPrice = tokGsPrice.nextToken();
				tokTmpColor = tokGsColor.nextToken();
				tokTmpSize = tokGsSize.nextToken();
				tmpTotPrice = Integer.parseInt(tokTmpPrice) * Integer.parseInt(tokTmpCount);  
				
				orderAddSql = "";
				orderAddSql = orderAddSql + "insert into order_detail (oi_no, gs_code, od_cnt, od_goods_price, od_total_price, od_color, od_size) values ( '";
				orderAddSql = orderAddSql + oi_no + "' , '";
				orderAddSql = orderAddSql + tokTmpCode + "' , '";
				orderAddSql = orderAddSql + tokTmpCount + "' , '";
				orderAddSql = orderAddSql + tokTmpPrice + "' , '";
				orderAddSql = orderAddSql + tmpTotPrice + "' , '";
				orderAddSql = orderAddSql + tokTmpColor + "' , '";
				orderAddSql = orderAddSql + tokTmpSize;
				orderAddSql = orderAddSql + "' ) ";
				pstmtOrder = conn.prepareStatement(orderAddSql);
				pstmtOrder.executeUpdate();
				//System.out.println(orderAddSql);
			}

		}catch(Exception e){
			
		}finally{
			try{
				if(pstmtOrder != null) pstmtOrder.close();
				if(conn != null) conn.close();
			}catch(Exception e){}
		}

		session.removeAttribute("CART");
		
		RequestDispatcher rd = request.getRequestDispatcher("/Dmyna/myshop/order/orderList.jsp");
		rd.forward(request, response);
		
	}

}

